package org.lease.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.lease.mapper.UserInfoMapper;
import org.lease.model.entity.UserInfo;
import org.lease.model.enums.BaseStatus;
import org.lease.model.query.UserInfoPageQuery;
import org.lease.service.UserInfoService;
import org.springframework.stereotype.Service;

@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Override
    public IPage<UserInfo> pageQuery(UserInfoPageQuery query) {
        Page<UserInfo> page = new Page<>(query.getCurrent(), query.getSize());
        BaseStatus status = query.getStatus();
        String phone = query.getPhone();
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(phone != null, UserInfo::getPhone, phone);
        wrapper.eq(status != null, UserInfo::getStatus, status);
        return page(page, wrapper);
    }

    @Override
    public boolean updateStatus(Long id, BaseStatus status) {
        LambdaUpdateWrapper<UserInfo> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(UserInfo::getStatus, status);
        wrapper.eq(UserInfo::getId, id);
        return update(wrapper);
    }

    // 根据手机号查询用户
    @Override
    public UserInfo getByPhone(String phone) {
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<UserInfo>().eq(UserInfo::getPhone, phone);
        return getOne(wrapper);
    }
}
